package com.company.hrmadmin.modules.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.hrmadmin.common.constant.ErrorEnum;
import com.company.hrmadmin.common.constant.emp.EmployeeFormEnum;
import com.company.hrmadmin.common.exception.GlobalException;
import com.company.hrmadmin.modules.domain.Department;
import com.company.hrmadmin.modules.domain.User;
import com.company.hrmadmin.modules.entity.dto.employee.DepNameDTO;
import com.company.hrmadmin.modules.entity.dto.employee.EmpTypeDTO;
import com.company.hrmadmin.modules.mapper.UserMapper;
import com.company.hrmadmin.modules.service.DepartmentService;
import com.company.hrmadmin.modules.mapper.DepartmentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * @author Xmirror
 * @description 针对表【bs_department(部门)】的数据库操作Service实现
 * @createDate 2022-03-18 15:38:49
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
        implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Department getParentDep(Integer depId) {
        Department department = departmentMapper.selectById(depId);
        if (department == null) return null;
        Integer parentId = department.getParentId();
        if (parentId == null) {
            return null;
        } else {
            return departmentMapper.selectById(parentId);
        }
    }

    @Override
    public List<Department> getChildrenDepList(Integer depId) {
        return departmentMapper.selectList(Wrappers.lambdaQuery(Department.class)
                .eq(Department::getParentId, depId));
    }

    @Override
    public List<Department> getDepRecListById(Integer depId) {
        return departmentMapper.selectDepListById(depId);
    }

    @Override
    public List<Department> getTopDepList() {
        return departmentMapper.selectTopDepList();
    }

    @Override
    public List<Department> getDepTreeList() {
        List<Department> list = this.getTopDepList();
        if (list == null || list.size() == 0) {
            return null;
        }
        List<Department> res = new LinkedList<>();
        for (Department department : list) {
            if (department.getParentId() == null) {
                res.add(department);
            }
        }
        for (Department re : res) {
            re.setList(this.getChildrenDepList(re.getId()));
        }
        return res;
    }

    @Override
    public List<Department> getDepListByCompanyId(Integer companyId) {
        List<Department> list = departmentMapper.selectList(Wrappers.lambdaQuery(Department.class)
                .eq(Department::getCompanyId, companyId));
        Optional.ofNullable(list).orElseThrow(() -> new GlobalException(ErrorEnum.DEPARTMENT_NOT_EXIST));
        return list;
    }

    @Override
    public List<DepNameDTO> getDepNameList(Integer userId) {
        User user = userMapper.selectById(userId);
        Optional.ofNullable(user).orElseThrow(() -> new GlobalException(ErrorEnum.USER_NOT_EXIST));
        Integer companyId = user.getCompanyId();
        List<Department> depList = this.getDepListByCompanyId(companyId);
        List<DepNameDTO> list = new ArrayList<>();
        if (depList != null && depList.size() != 0) {
            depList.forEach(department -> list.add(new DepNameDTO(department.getName(), department.getId())));
        }
        return list;
    }

    @Override
    public List<EmpTypeDTO> getEmpTypeList() {
        List<EmpTypeDTO> list = new ArrayList<>();
        list.add(new EmpTypeDTO(EmployeeFormEnum.FULL_TIME.getId(), EmployeeFormEnum.FULL_TIME.getName()));
        list.add(new EmpTypeDTO(EmployeeFormEnum.INTERN.getId(), EmployeeFormEnum.INTERN.getName()));
        list.add(new EmpTypeDTO(EmployeeFormEnum.TRY_OUT.getId(), EmployeeFormEnum.TRY_OUT.getName()));
        return list;
    }


}




